package hu.mapro.gwtui.client;

public class DelegateVisibility implements Visibility {

	Visibility delegate;

	Handlers h = Handlers.newInstance();
	
	Action a = new Action() {
		@Override
		public void perform() {
			h.fire();
		}
	};
	
	public DelegateVisibility(Visibility delegate) {
		setDelegate(delegate);
	}

	@Override
	public boolean isVisible() {
		return delegate.isVisible();
	}

	@Override
	public void addChangeHandler(Action action) {
		h.add(action);
	}
	
	public void setDelegate(Visibility delegate) {
		Visibility oldDelegate = this.delegate;
		this.delegate = delegate;
		this.delegate.addChangeHandler(a);
		if (oldDelegate!=null) {
			oldDelegate.removeChangeHandler(a);
		}
		a.perform();
	}

	@Override
	public void removeChangeHandler(Action action) {
		h.remove(action);
	}
	
}
